package com.itcast.www.im.ws;

import java.util.*;

public class ChatEndPointManager {

    /**
     * 记录在线终端
     */
    private static Map<String, ChatEndPoint> onLines = new HashMap<>();

    /**
     * 添加在线终端
     *
     * @param name
     * @param chatEndPoint
     */
    public static void add(String name, ChatEndPoint chatEndPoint) {
        onLines.put(name, chatEndPoint);
    }


    /**
     * 获取对应终端
     *
     * @param name
     */
    public static ChatEndPoint get(String name) {
        ChatEndPoint chatEndPoint = onLines.get(name);
        return chatEndPoint;
    }

    /**
     * 删除在线终端
     *
     * @param name
     */
    public static ChatEndPoint del(String name) {
        if (onLines.containsKey(name)) {
            return onLines.remove(name);
        }
        return null;
    }


    public static List<String> getOnLines() {
        return new ArrayList<String>(onLines.keySet());
    }


    /**
     * 获取其他 终端
     *
     * @param name
     * @return
     */
    public static List<ChatEndPoint> getOtherOnLines(String name) {
        List<ChatEndPoint> list = new ArrayList<>();
        Iterator<String> iterator = onLines.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            if (!name.equals(key)) {
                list.add(onLines.get(key));
            }
        }
        return list;
    }

    /**
     * 获取所有 终端
     * @return
     */
    public static List<ChatEndPoint> getAllOnLines() {
        List<ChatEndPoint> list = new ArrayList<>();
        Iterator<String> iterator = onLines.keySet().iterator();
        while (iterator.hasNext()) {
            list.add(onLines.get(iterator.next()));
        }
        return list;
    }
}
